'use strict';
const Controller = require('egg').Controller;
const { setMenuActive } = require('../utils');

class ListController extends Controller {
  async index() {
    const ctx = this.ctx;
    const data = {};
    const targetURLNode = ctx.targetURLNode;

    const list = await ctx.service.cms.getContentList(targetURLNode.urlNode.id);
    data.title = `红塔期货-${targetURLNode.urlNode.label}`;
    data.menu = setMenuActive(ctx.app.menus, targetURLNode.nodeArr[targetURLNode.nodeArr.length - 1].label);
    data.menuId = targetURLNode.urlNode.id;
    data.baseDetailUrl = targetURLNode.urlNode.menu.href;
    data.sidebarOptions = targetURLNode.nodeArr[targetURLNode.nodeArr.length - 1];
    data.list = list.rows;
    data.breadcrumb = targetURLNode.nodeArr.reverse();
    data.pages = {
      cur: ctx.query.page,
      total: list.total,
    };

    if (parseInt(targetURLNode.urlNode.menu.htmlType) === 1) {
      await this.ctx.render('/templates/list1.njk', data);
    } else if (parseInt(targetURLNode.urlNode.menu.htmlType) === 2) {
      await this.ctx.render('/templates/list2.njk', data);
    }
  }
}

module.exports = ListController;
